********************************************************************************
*DYNAMIC IMPACTS OF PRICING GROUNDWATER
*Bruno, Jessoe, Hanemann in JAERE
*
*THIS CODE IS FOR THE EVENT STUDY ELASTICITY
*Estimates percentage change in Q for each post-treatment year
*Combine with excel sheet calculations to create Table 4 in Appendix
********************************************************************************
clear all
capture log close
set more off

*SELECT OUTPUT DATE
global outputdate = "20231126"		 

*SET DIRECTORY
cd  "D:\Ellen\Dropbox\Pajaro_AgInnovation" 

use "Data\Parcel_clean_yearrun_20230626.dta", clear

*Balance the panel
bysort parcelnum: gen ntime = [_N]
sum ntime
keep if ntime==`r(max)'

drop if year_run >10

********************************************************************************
*1.1 Generate dummy variables
********************************************************************************

*Generate event periods 
	*year_run=6 is Q42010-Q32011
gen event_time = 0
sum year_run
forvalues j = 1/`r(max)' {
replace event_time = -`j' if year_run == 6 - `j'
replace event_time = `j' if year_run == 6 + `j'
}
sum year_run

*Generate 5 pre period interaction terms
forvalues j = 1/`r(max)' {
gen minus`j' = 0
replace minus`j' = 1 if inside == 1 & event_time == -`j'
}

*Generate 6 post period interaction terms
forvalues j = 1/`r(max)' {
gen plus`j' = 0
replace plus`j' = 1 if inside == 1 & event_time == `j'
}

*Generate treatment event interaction
**The date of the price split = 2010 
generate event_treatment = 0
replace event_treatment  = 1 if inside == 1 & event_time==0


*Drop missing variables
foreach var of varlist plus* minus* {
summ `var'
   if `r(sum)' == 0  {
            drop `var'
           }
       else { 
               }
         }
drop if year_run==5


********************************************************************************
*MAIN UNCONDTIIONAL EVENT STUDY DROPPING 2010
********************************************************************************
reg extraction i.year_run inside event_treatment plus* minus5 minus4 minus3 minus2, cluster(parcelnum)

*Produces Column B in "Table4_elasticity_calc.xls" 

*predict what extraction would have been in absence of treatment
predict extract_2011 if year_run==6 & inside ==1
preserve
replace event_treatment=0
predict extract_cf_2011 if year_run == 6 & inside ==1
gen change_2011 = _b[event_treatment]/extract_cf_2011
sum change_2011 
restore

predict extract_2012 if year_run==7 & inside ==1
preserve
replace event_treatment=0
predict extract_cf_2012 if year_run==7 & inside ==1
gen change_2012 = _b[plus1]/extract_cf_2012
sum change_2012 
restore

predict extract_2013 if year_run==8 & inside ==1
preserve
replace event_treatment=0
predict extract_cf_2013 if year_run==8 & inside ==1
gen change_2013 = _b[plus2]/extract_cf_2013
sum change_2013 
restore

predict extract_2014 if year_run==9 & inside ==1
preserve
replace event_treatment=0
predict extract_cf_2014 if year_run==9 & inside ==1
gen change_2014 = _b[plus3]/extract_cf_2014
sum change_2014 
restore

predict extract_2015 if year_run==10 & inside ==1
preserve
replace event_treatment=0
predict extract_cf_2015 if year_run==10 & inside ==1
gen change_2015 = _b[plus4]/extract_cf_2015
sum change_2015 
restore

